
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>[编写用户程序] · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.3">
        
        
        
    
    
    <link rel="stylesheet" href="../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-chapter-fold/chapter-fold.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-katex/katex.min.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-alerts/style.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-emphasize/plugin.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-prism/prism-tomorrow.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-mermaid-gb3/mermaid/mermaid.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    


    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="part1_1.html" />
    
    
    <link rel="prev" href="introduction.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="../chapter0/introduction.html">
            
                <a href="../chapter0/introduction.html">
            
                    
                    第零章：实验环境说明
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="../chapter1/introduction.html">
            
                <a href="../chapter1/introduction.html">
            
                    
                    第一章：独立式可执行程序
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.3.1" data-path="../chapter1/part1.html">
            
                <a href="../chapter1/part1.html">
            
                    
                    [安装 nightly rust]
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.2" data-path="../chapter1/part2.html">
            
                <a href="../chapter1/part2.html">
            
                    
                    使用包管理器 cargo 创建 rust binary 项目
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.3" data-path="../chapter1/part3.html">
            
                <a href="../chapter1/part3.html">
            
                    
                    移除标准库依赖
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.4" data-path="../chapter1/part4.html">
            
                <a href="../chapter1/part4.html">
            
                    
                    [移除 runtime 依赖]
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3.5" data-path="../chapter1/part5.html">
            
                <a href="../chapter1/part5.html">
            
                    
                    总结与展望
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.4" data-path="../chapter2/introduction.html">
            
                <a href="../chapter2/introduction.html">
            
                    
                    第二章：最小化内核
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="../chapter2/part1.html">
            
                <a href="../chapter2/part1.html">
            
                    
                    使用目标三元组描述目标平台
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="../chapter2/part2.html">
            
                <a href="../chapter2/part2.html">
            
                    
                    编译、生成内核镜像
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.3" data-path="../chapter2/part3.html">
            
                <a href="../chapter2/part3.html">
            
                    
                    使用链接脚本指定内存布局
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.4" data-path="../chapter2/part4.html">
            
                <a href="../chapter2/part4.html">
            
                    
                    [重写程序入口点 -start]
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.5" data-path="../chapter2/part5.html">
            
                <a href="../chapter2/part5.html">
            
                    
                    [使用 Qemu 运行内核]
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.6" data-path="../chapter2/part6.html">
            
                <a href="../chapter2/part6.html">
            
                    
                    封装 SBI 接口
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.7" data-path="../chapter2/part7.html">
            
                <a href="../chapter2/part7.html">
            
                    
                    [实现格式化输出]
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.8" data-path="../chapter2/part8.html">
            
                <a href="../chapter2/part8.html">
            
                    
                    总结与展望
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="../chapter3/introduction.html">
            
                <a href="../chapter3/introduction.html">
            
                    
                    第三章：中断
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" data-path="../chapter3/part1.html">
            
                <a href="../chapter3/part1.html">
            
                    
                    rv64 中断介绍
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2" data-path="../chapter3/part2.html">
            
                <a href="../chapter3/part2.html">
            
                    
                    [手动触发断点中断]
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.3" data-path="../chapter3/part3.html">
            
                <a href="../chapter3/part3.html">
            
                    
                    程序运行上下文环境
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.4" data-path="../chapter3/part4.html">
            
                <a href="../chapter3/part4.html">
            
                    
                    [实现上下文环境保存与恢复]
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.5" data-path="../chapter3/part5.html">
            
                <a href="../chapter3/part5.html">
            
                    
                    [时钟中断]
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.6" data-path="../chapter3/part6.html">
            
                <a href="../chapter3/part6.html">
            
                    
                    总结与展望
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="../chapter4/introduction.html">
            
                <a href="../chapter4/introduction.html">
            
                    
                    第四章：内存管理
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.6.1" data-path="../chapter4/part1.html">
            
                <a href="../chapter4/part1.html">
            
                    
                    [物理内存探测与管理]
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.2" data-path="../chapter4/part2.html">
            
                <a href="../chapter4/part2.html">
            
                    
                    [动态内存分配]
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.6.3" data-path="../chapter4/part3.html">
            
                <a href="../chapter4/part3.html">
            
                    
                    总结与展望
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="../chapter5/introduction.html">
            
                <a href="../chapter5/introduction.html">
            
                    
                    第五章：内存虚拟化
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.7.1" data-path="../chapter5/part1.html">
            
                <a href="../chapter5/part1.html">
            
                    
                    页表：从虚拟内存到物理内存
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.2" data-path="../chapter5/part2.html">
            
                <a href="../chapter5/part2.html">
            
                    
                    [内核初始映射]
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.3" data-path="../chapter5/part3.html">
            
                <a href="../chapter5/part3.html">
            
                    
                    内核重映射
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.4" data-path="../chapter5/part4.html">
            
                <a href="../chapter5/part4.html">
            
                    
                    内核重映射实现之一：页表
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.5" data-path="../chapter5/part5.html">
            
                <a href="../chapter5/part5.html">
            
                    
                    内核重映射实现之二：MemorySet
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.6" data-path="../chapter5/part6.html">
            
                <a href="../chapter5/part6.html">
            
                    
                    [内核重映射实现之三：完结]
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7.7" data-path="../chapter5/part7.html">
            
                <a href="../chapter5/part7.html">
            
                    
                    总结与展望
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="../chapter6/introduction.html">
            
                <a href="../chapter6/introduction.html">
            
                    
                    第六章：内核线程
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.8.1" data-path="../chapter6/part1.html">
            
                <a href="../chapter6/part1.html">
            
                    
                    线程状态与保存
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.2" data-path="../chapter6/part2.html">
            
                <a href="../chapter6/part2.html">
            
                    
                    线程切换
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.3" data-path="../chapter6/part3.html">
            
                <a href="../chapter6/part3.html">
            
                    
                    内核线程初始化
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.4" data-path="../chapter6/part4.html">
            
                <a href="../chapter6/part4.html">
            
                    
                    [内核线程创建与切换测试]
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8.5" data-path="../chapter6/part5.html">
            
                <a href="../chapter6/part5.html">
            
                    
                    总结与展望
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.9" data-path="../chapter7/introduction.html">
            
                <a href="../chapter7/introduction.html">
            
                    
                    第七章：线程调度
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.9.1" data-path="../chapter7/part1.html">
            
                <a href="../chapter7/part1.html">
            
                    
                    线程池与线程管理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.2" data-path="../chapter7/part2.html">
            
                <a href="../chapter7/part2.html">
            
                    
                    内核调度线程 idle
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.3" data-path="../chapter7/part3.html">
            
                <a href="../chapter7/part3.html">
            
                    
                    线程调度之 Round Robin 算法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.4" data-path="../chapter7/part4.html">
            
                <a href="../chapter7/part4.html">
            
                    
                    [线程调度测试]
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9.5" data-path="../chapter7/part5.html">
            
                <a href="../chapter7/part5.html">
            
                    
                    总结与展望
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.10" data-path="introduction.html">
            
                <a href="introduction.html">
            
                    
                    第八章：进程
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter active" data-level="1.10.1" data-path="part1.html">
            
                <a href="part1.html">
            
                    
                    [编写用户程序]
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.2" data-path="part1_1.html">
            
                <a href="part1_1.html">
            
                    
                    合并内核与应用程序
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.3" data-path="part2.html">
            
                <a href="part2.html">
            
                    
                    在内核中实现系统调用
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.4" data-path="part3.html">
            
                <a href="part3.html">
            
                    
                    创建虚拟内存空间
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.5" data-path="part4.html">
            
                <a href="part4.html">
            
                    
                    [创建进程]
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10.6" data-path="part5.html">
            
                <a href="part5.html">
            
                    
                    总结与展望
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.11" data-path="../chapter9/introduction.html">
            
                <a href="../chapter9/introduction.html">
            
                    
                    第九章：文件系统
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.11.1" data-path="../chapter9/part1.html">
            
                <a href="../chapter9/part1.html">
            
                    
                    [使用文件系统]
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11.2" data-path="../chapter9/part2.html">
            
                <a href="../chapter9/part2.html">
            
                    
                    [实现记事本]
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11.3" data-path="../chapter9/part3.html">
            
                <a href="../chapter9/part3.html">
            
                    
                    [实现终端]
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11.4" data-path="../chapter9/part4.html">
            
                <a href="../chapter9/part4.html">
            
                    
                    [文件读写]
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.11.5" data-path="../chapter9/part5.html">
            
                <a href="../chapter9/part5.html">
            
                    
                    总结与展望
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.12" data-path="../chapter10/introduction.html">
            
                <a href="../chapter10/introduction.html">
            
                    
                    第十章：同步互斥
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13" data-path="../exercise/introduction.html">
            
                <a href="../exercise/introduction.html">
            
                    
                    练习题
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.13.1" data-path="../exercise/part1.html">
            
                <a href="../exercise/part1.html">
            
                    
                    1. 中断异常
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13.2" data-path="../exercise/part2.html">
            
                <a href="../exercise/part2.html">
            
                    
                    2. 物理内存管理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13.3" data-path="../exercise/part3.html">
            
                <a href="../exercise/part3.html">
            
                    
                    3. 虚拟内存管理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13.4" data-path="../exercise/part4.html">
            
                <a href="../exercise/part4.html">
            
                    
                    4. 线程管理
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13.5" data-path="../exercise/part5.html">
            
                <a href="../exercise/part5.html">
            
                    
                    5. 用户进程（+ 虚拟内存管理 + 线程管理）
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13.6" data-path="../exercise/part6.html">
            
                <a href="../exercise/part6.html">
            
                    
                    6. CPU 调度
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13.7" data-path="../exercise/part7.html">
            
                <a href="../exercise/part7.html">
            
                    
                    7. 同步互斥
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.13.8" data-path="../exercise/part8.html">
            
                <a href="../exercise/part8.html">
            
                    
                    8. 文件系统
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.14" data-path="../appendix/introduction.html">
            
                <a href="../appendix/introduction.html">
            
                    
                    附录
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.14.1" data-path="../appendix/inline_asm.html">
            
                <a href="../appendix/inline_asm.html">
            
                    
                    内联汇编
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.14.2" data-path="../appendix/install_rust.html">
            
                <a href="../appendix/install_rust.html">
            
                    
                    安装 rust
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >[编写用户程序]</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h2 id="&#x7F16;&#x5199;&#x7528;&#x6237;&#x7A0B;&#x5E8F;">&#x7F16;&#x5199;&#x7528;&#x6237;&#x7A0B;&#x5E8F;</h2>
<ul>
<li><a href="https://github.com/rcore-os/rCore_tutorial/tree/ch8-pa1" target="_blank">&#x4EE3;&#x7801;</a></li>
</ul>
<p>&#x672C;&#x8282;&#x7684;&#x5DE5;&#x4F5C;&#x5F88;&#x7C7B;&#x4F3C;<a href="../chapter1/part4.html">&#x7B2C;&#x4E00;&#x7AE0;&#x7B2C;&#x56DB;&#x8282;<strong>&#x79FB;&#x9664; runtime &#x4F9D;&#x8D56;</strong></a>&#x7684;&#x5DE5;&#x4F5C;&#xFF0C;&#x4F46;&#x533A;&#x522B;&#x662F;&#xFF0C;<a href="../chapter1/part4.html">&#x7B2C;&#x4E00;&#x7AE0;&#x7B2C;&#x56DB;&#x8282;<strong>&#x79FB;&#x9664; runtime &#x4F9D;&#x8D56;</strong></a>&#x662F;&#x8981;&#x5B8C;&#x5168;&#x79FB;&#x9664;&#x5BF9; runtime &#x7684;&#x9700;&#x6C42;&#xFF0C;&#x4EE5;&#x6784;&#x9020; OS&#xFF1B;&#x800C;&#x672C;&#x8282;&#x9700;&#x8981;&#x5B9E;&#x73B0;&#x4E00;&#x4E2A;&#x652F;&#x6301; U Mode &#x5E94;&#x7528;&#x7A0B;&#x5E8F;&#x7684;&#x6700;&#x5C0F; runtime &#xFF0C;&#x8FD9;&#x4E2A; runtime &#x4EC5;&#x4EC5;&#x9700;&#x8981;&#x652F;&#x6301;&#x5F88;&#x5C11;&#x7CFB;&#x7EDF;&#x8C03;&#x7528;&#x8BBF;&#x95EE;&#x548C;&#x57FA;&#x672C;&#x7684;&#x52A8;&#x6001;&#x5185;&#x5B58;&#x5206;&#x914D;&#x3002;&#x867D;&#x7136;&#x6709;&#x533A;&#x522B;&#xFF0C;&#x5F88;&#x591A;&#x672C;&#x8282;&#x5F88;&#x591A;&#x4EE3;&#x7801;&#x90FD;&#x53EF;&#x4EE5;&#x76F4;&#x63A5;&#x53C2;&#x8003;<a href="../chapter1/part4.html">&#x7B2C;&#x4E00;&#x7AE0;&#x7B2C;&#x56DB;&#x8282;<strong>&#x79FB;&#x9664; runtime &#x4F9D;&#x8D56;</strong></a>&#x7684;&#x8BBE;&#x8BA1;&#x601D;&#x8DEF;&#x548C;&#x4EE3;&#x7801;&#x3002;</p>
<p>&#x6211;&#x4EEC;&#x7684;&#x7528;&#x6237;&#x7A0B;&#x5E8F;&#x4E00;&#x822C;&#x5728; CPU &#x7684;&#x7528;&#x6237;&#x6001; (U Mode) &#x4E0B;&#x6267;&#x884C;&#xFF0C;&#x800C;&#x5B83;&#x53EA;&#x80FD;&#x901A;&#x8FC7;&#x6267;&#x884C; <code>ecall</code> &#x6307;&#x4EE4;&#xFF0C;&#x89E6;&#x53D1; <code>Environment call from U-mode</code> &#x5F02;&#x5E38; l &#x6765;&#x53D1;&#x51FA;&#x7CFB;&#x7EDF;&#x670D;&#x52A1;&#x8BF7;&#x6C42;&#xFF0C;&#x6B64;&#x65F6; CPU &#x8FDB;&#x5165;&#x5185;&#x6838;&#x6001; (S Mode) &#xFF0C;OS &#x901A;&#x8FC7;&#x4E2D;&#x65AD;&#x670D;&#x52A1;&#x4F8B;&#x7A0B;&#x6536;&#x5230;&#x8BF7;&#x6C42;&#xFF0C;&#x6267;&#x884C;&#x76F8;&#x5E94;&#x5185;&#x6838;&#x670D;&#x52A1;&#xFF0C;&#x5E76;&#x8FD4;&#x56DE;&#x5230; U Mode&#x3002;</p>
<p>&#x8FD9;&#x4E00;&#x7AE0;&#x4E2D;&#xFF0C;&#x7B80;&#x5355;&#x8D77;&#x89C1;&#xFF0C;&#x5185;&#x6838;&#x548C;&#x7528;&#x6237;&#x7A0B;&#x5E8F;&#x7EA6;&#x5B9A;&#x4E24;&#x4E2A;&#x7CFB;&#x7EDF;&#x8C03;&#x7528;</p>
<ul>
<li>&#x5728;&#x5C4F;&#x5E55;&#x4E0A;&#x8F93;&#x51FA;&#x4E00;&#x4E2A;&#x5B57;&#x7B26;&#xFF0C;&#x7CFB;&#x7EDF;&#x8C03;&#x7528; <span class="katex"><span class="katex-mathml"><math><semantics><mrow><mtext><mi mathvariant="normal">i</mi><mi mathvariant="normal">d</mi></mtext><mo>=</mo><mn>6</mn><mn>4</mn></mrow><annotation encoding="application/x-tex">\text{id}=64</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="strut" style="height:0.69444em;"></span><span class="strut bottom" style="height:0.69444em;vertical-align:0em;"></span><span class="base textstyle uncramped"><span class="mord text textstyle uncramped"><span class="mord mathrm">id</span></span><span class="mrel">=</span><span class="mord mathrm">6</span><span class="mord mathrm">4</span></span></span></span></li>
<li>&#x9000;&#x51FA;&#x7528;&#x6237;&#x7EBF;&#x7A0B;&#xFF0C;&#x7CFB;&#x7EDF;&#x8C03;&#x7528; <span class="katex"><span class="katex-mathml"><math><semantics><mrow><mtext><mi mathvariant="normal">i</mi><mi mathvariant="normal">d</mi></mtext><mo>=</mo><mn>9</mn><mn>7</mn></mrow><annotation encoding="application/x-tex">\text{id}=97</annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="strut" style="height:0.69444em;"></span><span class="strut bottom" style="height:0.69444em;vertical-align:0em;"></span><span class="base textstyle uncramped"><span class="mord text textstyle uncramped"><span class="mord mathrm">id</span></span><span class="mrel">=</span><span class="mord mathrm">9</span><span class="mord mathrm">7</span></span></span></span></li>
</ul>
<h3 id="&#x521B;&#x5EFA;&#x7528;&#x6237;&#x7A0B;&#x5E8F;&#x6A21;&#x677F;">&#x521B;&#x5EFA;&#x7528;&#x6237;&#x7A0B;&#x5E8F;&#x6A21;&#x677F;</h3>
<p>&#x6211;&#x4EEC;&#x7684;&#x5185;&#x6838;&#x80FD;&#x7ED9;&#x7A0B;&#x5E8F;&#x63D0;&#x4F9B;&#x7684;&#x552F;&#x4E00;&#x652F;&#x6301;&#x5C31;&#x662F;&#x4E24;&#x4E2A;&#x7B80;&#x5355;&#x7684;&#x7CFB;&#x7EDF;&#x8C03;&#x7528;&#x3002;</p>
<p>&#x6240;&#x4EE5;&#x6211;&#x4EEC;&#x7684;&#x7528;&#x6237;&#x7A0B;&#x5E8F;&#x57FA;&#x672C;&#x8FD8;&#x662F;&#x8981;&#x4F7F;&#x7528;&#x524D;&#x4E24;&#x7AE0;&#x7684;&#x65B9;&#x6CD5;&#xFF0C;&#x4E0D;&#x540C;&#x7684;&#x5219;&#x662F;&#x8981;&#x628A;&#x7CFB;&#x7EDF;&#x8C03;&#x7528;&#x52A0;&#x5165;&#x8FDB;&#x53BB;&#x3002;</p>
<p>&#x521B;&#x5EFA; <code>usr</code> &#x76EE;&#x5F55;&#xFF0C;&#x5E76;&#x5728; <code>usr</code> &#x76EE;&#x5F55;&#x4E0B;&#x4F7F;&#x7528; Cargo &#x65B0;&#x5EFA;&#x4E00;&#x4E2A;&#x4E8C;&#x8FDB;&#x5236;&#x9879;&#x76EE;&#xFF0C;&#x518D;&#x5220;&#x9664;&#x6389;&#x9ED8;&#x8BA4;&#x751F;&#x6210;&#x7684; <code>usr/rust/src/main.rs</code> &#x3002;</p>
<pre class="language-"><code class="lang-bash">$ <span class="token function">mkdir</span> usr<span class="token punctuation">;</span> <span class="token builtin class-name">cd</span> usr
$ cargo new  rust --bin
$ <span class="token function">rm</span> usr/rust/src/main.rs
</code></pre>
<p>&#x52A0;&#x4E0A;&#x5DE5;&#x5177;&#x94FE;</p>
<pre class="language-"><code class="lang-rust"><span class="token comment">// usr/rust/rust-toolchain</span>
nightly
</code></pre>
<h3 id="&#x5EFA;&#x7ACB;&#x6700;&#x5C0F;-runtime-&#x7CFB;&#x7EDF;">&#x5EFA;&#x7ACB;&#x6700;&#x5C0F; Runtime &#x7CFB;&#x7EDF;</h3>
<h4 id="&#x8BBF;&#x95EE;&#x7CFB;&#x7EDF;&#x8C03;&#x7528;">&#x8BBF;&#x95EE;&#x7CFB;&#x7EDF;&#x8C03;&#x7528;</h4>
<p>&#x6211;&#x4EEC;&#x5148;&#x6765;&#x770B;&#x8BBF;&#x95EE;&#x7CFB;&#x7EDF;&#x8C03;&#x7528;&#x7684;&#x5B9E;&#x73B0;&#xFF1A;</p>
<pre class="language-"><code class="lang-rust"><span class="token comment">// usr/rust/src/syscall.rs</span>

<span class="token keyword">enum</span> <span class="token type-definition class-name">SyscallId</span> <span class="token punctuation">{</span>
    <span class="token class-name">Write</span> <span class="token operator">=</span> <span class="token number">64</span><span class="token punctuation">,</span>
    <span class="token class-name">Exit</span> <span class="token operator">=</span> <span class="token number">93</span><span class="token punctuation">,</span>
<span class="token punctuation">}</span>

<span class="token attribute attr-name">#[inline(always)]</span>
<span class="token keyword">fn</span> <span class="token function-definition function">sys_call</span><span class="token punctuation">(</span>
    syscall_id<span class="token punctuation">:</span> <span class="token class-name">SyscallId</span><span class="token punctuation">,</span>
    arg0<span class="token punctuation">:</span> <span class="token keyword">usize</span><span class="token punctuation">,</span>
    arg1<span class="token punctuation">:</span> <span class="token keyword">usize</span><span class="token punctuation">,</span>
    arg2<span class="token punctuation">:</span> <span class="token keyword">usize</span><span class="token punctuation">,</span>
    arg3<span class="token punctuation">:</span> <span class="token keyword">usize</span><span class="token punctuation">,</span>
<span class="token punctuation">)</span> <span class="token punctuation">-&gt;</span> <span class="token keyword">i64</span> <span class="token punctuation">{</span>
    <span class="token keyword">let</span> id <span class="token operator">=</span> syscall_id <span class="token keyword">as</span> <span class="token keyword">usize</span><span class="token punctuation">;</span>
    <span class="token keyword">let</span> <span class="token keyword">mut</span> ret<span class="token punctuation">:</span> <span class="token keyword">i64</span><span class="token punctuation">;</span>
    <span class="token keyword">unsafe</span> <span class="token punctuation">{</span>
        <span class="token macro property">asm!</span><span class="token punctuation">(</span>
            <span class="token string">&quot;ecall&quot;</span>
            <span class="token punctuation">:</span> <span class="token string">&quot;={x10}&quot;</span><span class="token punctuation">(</span>ret<span class="token punctuation">)</span>
            <span class="token punctuation">:</span> <span class="token string">&quot;{x17}&quot;</span><span class="token punctuation">(</span>id<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token string">&quot;{x10}&quot;</span><span class="token punctuation">(</span>arg0<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token string">&quot;{x11}&quot;</span><span class="token punctuation">(</span>arg1<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token string">&quot;{x12}&quot;</span><span class="token punctuation">(</span>arg2<span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token string">&quot;{x13}&quot;</span><span class="token punctuation">(</span>arg3<span class="token punctuation">)</span>
            <span class="token punctuation">:</span> <span class="token string">&quot;memory&quot;</span>
            <span class="token punctuation">:</span> <span class="token string">&quot;volatile&quot;</span>
        <span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    ret
<span class="token punctuation">}</span>

<span class="token keyword">pub</span> <span class="token keyword">fn</span> <span class="token function-definition function">sys_write</span><span class="token punctuation">(</span>ch<span class="token punctuation">:</span> <span class="token keyword">u8</span><span class="token punctuation">)</span> <span class="token punctuation">-&gt;</span> <span class="token keyword">i64</span> <span class="token punctuation">{</span>
    <span class="token function">sys_call</span><span class="token punctuation">(</span><span class="token class-name">SyscallId</span><span class="token punctuation">::</span><span class="token class-name">Write</span><span class="token punctuation">,</span> ch <span class="token keyword">as</span> <span class="token keyword">usize</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>

<span class="token keyword">pub</span> <span class="token keyword">fn</span> <span class="token function-definition function">sys_exit</span><span class="token punctuation">(</span>code<span class="token punctuation">:</span> <span class="token keyword">usize</span><span class="token punctuation">)</span> <span class="token punctuation">-&gt;</span> <span class="token operator">!</span> <span class="token punctuation">{</span>
    <span class="token function">sys_call</span><span class="token punctuation">(</span><span class="token class-name">SyscallId</span><span class="token punctuation">::</span><span class="token class-name">Exit</span><span class="token punctuation">,</span> code<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">loop</span> <span class="token punctuation">{</span><span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x770B;&#x8D77;&#x6765;&#x5F88;&#x50CF;&#x5185;&#x6838;&#x4E2D; <code>src/sbi.rs</code> &#x83B7;&#x53D6; OpenSBI &#x670D;&#x52A1;&#x7684;&#x4EE3;&#x7801;&#x5BF9;&#x4E0D;&#x5BF9;&#xFF1F;&#x5176;&#x5B9E;&#x5185;&#x6838;&#x4E2D;&#x662F;&#x5728; S Mode &#x53BB;&#x83B7;&#x53D6; OpenSBI &#x63D0;&#x4F9B;&#x7684; M Mode &#x670D;&#x52A1;&#xFF1B;&#x8FD9;&#x91CC;&#x662F;&#x7528;&#x6237;&#x7A0B;&#x5E8F;&#x5728; U Mode &#x53BB;&#x83B7;&#x53D6;&#x5185;&#x6838;&#x63D0;&#x4F9B;&#x7684; S Mode &#x670D;&#x52A1;&#x3002;&#x6240;&#x4EE5;&#x770B;&#x8D77;&#x6765;&#x51E0;&#x4E4E;&#x4E00;&#x6A21;&#x4E00;&#x6837;&#x3002;</p>
<p>&#x76F8;&#x4FE1;&#x5185;&#x6838;&#x4F1A;&#x7ED9;&#x6211;&#x4EEC;&#x63D0;&#x4F9B;&#x8FD9;&#x4E24;&#x9879;&#x670D;&#x52A1;&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x5728;&#x7528;&#x6237;&#x7A0B;&#x5E8F;&#x4E2D;&#x653E;&#x5FC3;&#x7684;&#x8C03;&#x7528; <code>sys_write, sys_exit</code> &#x4E24;&#x51FD;&#x6570;&#x4E86;&#xFF01;</p>
<h4 id="&#x683C;&#x5F0F;&#x5316;&#x8F93;&#x51FA;">&#x683C;&#x5F0F;&#x5316;&#x8F93;&#x51FA;</h4>
<p>&#x63A5;&#x7740;&#x662F;&#x4E00;&#x4E9B;&#x6211;&#x4EEC;&#x5728;&#x6784;&#x5EFA;&#x6700;&#x5C0F;&#x5316;&#x5185;&#x6838;&#x65F6;&#x7528;&#x5230;&#x7684;&#x4EE3;&#x7801;&#xFF0C;&#x6709;&#x4E00;&#x4E9B;&#x53D8;&#x52A8;&#xFF0C;&#x4F46;&#x8FD9;&#x91CC;&#x4E0D;&#x591A;&#x52A0;&#x8D58;&#x8FF0;&#x3002;</p>
<p>&#x683C;&#x5F0F;&#x5316;&#x8F93;&#x51FA;&#x4EE3;&#x7801;&#xFF1A;</p>
<pre class="language-"><code class="lang-rust"><span class="token comment">// usr/rust/src/io.rs</span>

<span class="token keyword">use</span> <span class="token keyword">crate</span><span class="token module-declaration namespace"><span class="token punctuation">::</span>syscall<span class="token punctuation">::</span></span>sys_write<span class="token punctuation">;</span>
<span class="token keyword">use</span> <span class="token namespace">core<span class="token punctuation">::</span>fmt<span class="token punctuation">::</span></span><span class="token punctuation">{</span><span class="token keyword">self</span><span class="token punctuation">,</span> <span class="token class-name">Write</span><span class="token punctuation">}</span><span class="token punctuation">;</span>

<span class="token keyword">pub</span> <span class="token keyword">fn</span> <span class="token function-definition function">putchar</span><span class="token punctuation">(</span>ch<span class="token punctuation">:</span> <span class="token keyword">char</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token comment">// &#x8FD9;&#x91CC; OpenSBI &#x63D0;&#x4F9B;&#x7684; console_putchar &#x4E0D;&#x5B58;&#x5728;&#x4E86;</span>
    <span class="token comment">// &#x7136;&#x800C;&#x6211;&#x4EEC;&#x6709;&#x4E86;&#x65B0;&#x7684;&#x4F9D;&#x9760;&#xFF1A;sys_write</span>
    <span class="token function">sys_write</span><span class="token punctuation">(</span>ch <span class="token keyword">as</span> <span class="token keyword">u8</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
<span class="token comment">//&#x5176;&#x4ED6;&#x90E8;&#x5206;&#x4E0E;os/src/io.rs &#x4E00;&#x6837;</span>
<span class="token punctuation">...</span><span class="token punctuation">...</span>
</code></pre>
<h4 id="&#x8BED;&#x4E49;&#x9879;&#x652F;&#x6301;">&#x8BED;&#x4E49;&#x9879;&#x652F;&#x6301;</h4>
<p>&#x8BED;&#x4E49;&#x9879;&#x4EE3;&#x7801;&#xFF1A;</p>
<pre class="language-"><code class="lang-rust"><span class="token comment">// usr/rust/src/lang_items.rs</span>
<span class="token punctuation">...</span><span class="token punctuation">...</span>
<span class="token keyword">use</span> <span class="token keyword">crate</span><span class="token punctuation">::</span><span class="token constant">DYNAMIC_ALLOCATOR</span><span class="token punctuation">;</span>
<span class="token comment">// &#x521D;&#x59CB;&#x5316;&#x7528;&#x6237;&#x5806;&#xFF0C;&#x7528;&#x4E8E;U Mode&#x4E2D;&#x52A8;&#x6001;&#x5185;&#x5B58;&#x5206;&#x914D;</span>
<span class="token keyword">fn</span> <span class="token function-definition function">init_heap</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">const</span> <span class="token constant">HEAP_SIZE</span><span class="token punctuation">:</span> <span class="token keyword">usize</span> <span class="token operator">=</span> <span class="token number">0x1000</span><span class="token punctuation">;</span>
    <span class="token keyword">static</span> <span class="token keyword">mut</span> <span class="token constant">HEAP</span><span class="token punctuation">:</span> <span class="token punctuation">[</span><span class="token keyword">u8</span><span class="token punctuation">;</span> <span class="token constant">HEAP_SIZE</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">;</span> <span class="token constant">HEAP_SIZE</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
    <span class="token keyword">unsafe</span> <span class="token punctuation">{</span>
        <span class="token constant">DYNAMIC_ALLOCATOR</span><span class="token punctuation">.</span><span class="token function">lock</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">init</span><span class="token punctuation">(</span><span class="token constant">HEAP</span><span class="token punctuation">.</span><span class="token function">as_ptr</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">as</span> <span class="token keyword">usize</span><span class="token punctuation">,</span> <span class="token constant">HEAP_SIZE</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token attribute attr-name">#[panic_handler]</span>
<span class="token keyword">fn</span> <span class="token function-definition function">panic</span><span class="token punctuation">(</span>_info<span class="token punctuation">:</span> <span class="token operator">&amp;</span><span class="token class-name">PanicInfo</span><span class="token punctuation">)</span> <span class="token punctuation">-&gt;</span> <span class="token operator">!</span> <span class="token punctuation">{</span>
    <span class="token keyword">let</span> location <span class="token operator">=</span> _info<span class="token punctuation">.</span><span class="token function">location</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">unwrap</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">let</span> message <span class="token operator">=</span> _info<span class="token punctuation">.</span><span class="token function">message</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">unwrap</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token macro property">println!</span><span class="token punctuation">(</span>
        <span class="token string">&quot;\nPANIC in {} at line {} \n\t{}&quot;</span><span class="token punctuation">,</span>
        location<span class="token punctuation">.</span><span class="token function">file</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
        location<span class="token punctuation">.</span><span class="token function">line</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
        message
    <span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">loop</span> <span class="token punctuation">{</span><span class="token punctuation">}</span>
<span class="token punctuation">}</span>

<span class="token comment">// &#x8FD9;&#x91CC;&#x662F;&#x7A0B;&#x5E8F;&#x5165;&#x53E3;</span>
<span class="token comment">// &#x8C03;&#x7528; main &#x51FD;&#x6570;&#xFF0C;&#x5E76;&#x5229;&#x7528; sys_exit &#x7CFB;&#x7EDF;&#x8C03;&#x7528;&#x9000;&#x51FA;</span>
<span class="token attribute attr-name">#[no_mangle]</span>
<span class="token keyword">pub</span> <span class="token keyword">extern</span> <span class="token string">&quot;C&quot;</span> <span class="token keyword">fn</span> <span class="token function-definition function">_start</span><span class="token punctuation">(</span>_args<span class="token punctuation">:</span> <span class="token keyword">isize</span><span class="token punctuation">,</span> _argv<span class="token punctuation">:</span> <span class="token operator">*</span><span class="token keyword">const</span> <span class="token keyword">u8</span><span class="token punctuation">)</span> <span class="token punctuation">-&gt;</span> <span class="token operator">!</span> <span class="token punctuation">{</span>
    <span class="token function">init_heap</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token function">sys_exit</span><span class="token punctuation">(</span><span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
<span class="token punctuation">}</span>

<span class="token attribute attr-name">#[no_mangle]</span>
<span class="token keyword">pub</span> <span class="token keyword">extern</span> <span class="token keyword">fn</span> <span class="token function-definition function">abort</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token macro property">panic!</span><span class="token punctuation">(</span><span class="token string">&quot;abort&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token attribute attr-name">#[lang = <span class="token string">&quot;oom&quot;</span>]</span>
<span class="token keyword">fn</span> <span class="token function-definition function">oom</span><span class="token punctuation">(</span>_<span class="token punctuation">:</span> <span class="token class-name">Layout</span><span class="token punctuation">)</span> <span class="token punctuation">-&gt;</span> <span class="token operator">!</span> <span class="token punctuation">{</span>
    <span class="token macro property">panic!</span><span class="token punctuation">(</span><span class="token string">&quot;out of memory!&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x770B;&#x8D77;&#x6765;&#x5F88;&#x50CF;&#x5185;&#x6838;&#x4E2D; <code>src/lang_item.rs</code> &#x83B7;&#x53D6; OpenSBI &#x670D;&#x52A1;&#x7684;&#x4EE3;&#x7801;&#x5BF9;&#x4E0D;&#x5BF9;&#xFF1F;&#x5176;&#x5B9E;&#x5185;&#x6838;&#x4E2D;&#x662F;&#x5728; S Mode &#x53BB;&#x83B7;&#x53D6; OpenSBI &#x63D0;&#x4F9B;&#x7684; M Mode &#x670D;&#x52A1;&#xFF1B;&#x8FD9;&#x91CC;&#x662F;&#x7528;&#x6237;&#x7A0B;&#x5E8F;&#x5728; U Mode &#x53BB;&#x83B7;&#x53D6;&#x5185;&#x6838;&#x63D0;&#x4F9B;&#x7684; S Mode &#x670D;&#x52A1;&#x3002;&#x6240;&#x4EE5;&#x770B;&#x8D77;&#x6765;&#x51E0;&#x4E4E;&#x4E00;&#x6A21;&#x4E00;&#x6837;&#x3002;</p>
<h4 id="&#x5F62;&#x6210;-runtime-lib">&#x5F62;&#x6210; runtime lib</h4>
<p>&#x8FD8;&#x6709; <code>lib.rs</code>&#xFF1A;</p>
<pre class="language-"><code class="lang-rust"><span class="token comment">// usr/rust/Cargo.toml</span>

<span class="token punctuation">[</span>dependencies<span class="token punctuation">]</span>
buddy_system_allocator <span class="token operator">=</span> <span class="token string">&quot;0.3&quot;</span>

<span class="token comment">// usr/rust/src/lib.rs</span>

<span class="token attribute attr-name">#![no_std]</span>
<span class="token attribute attr-name">#![feature(asm)]</span>
<span class="token attribute attr-name">#![feature(lang_items)]</span>
<span class="token attribute attr-name">#![feature(panic_info_message)]</span>
<span class="token attribute attr-name">#![feature(linkage)]</span>

<span class="token keyword">extern</span> <span class="token keyword">crate</span> <span class="token module-declaration namespace">alloc</span><span class="token punctuation">;</span>

<span class="token attribute attr-name">#[macro_use]</span>
<span class="token keyword">pub</span> <span class="token keyword">mod</span> <span class="token module-declaration namespace">io</span><span class="token punctuation">;</span>

<span class="token keyword">pub</span> <span class="token keyword">mod</span> <span class="token module-declaration namespace">syscall</span><span class="token punctuation">;</span>
<span class="token keyword">pub</span> <span class="token keyword">mod</span> <span class="token module-declaration namespace">lang_items</span><span class="token punctuation">;</span>

<span class="token keyword">use</span> <span class="token namespace">buddy_system_allocator<span class="token punctuation">::</span></span><span class="token class-name">LockedHeap</span><span class="token punctuation">;</span>

<span class="token attribute attr-name">#[global_allocator]</span>
<span class="token keyword">static</span> <span class="token constant">DYNAMIC_ALLOCATOR</span><span class="token punctuation">:</span> <span class="token class-name">LockedHeap</span> <span class="token operator">=</span> <span class="token class-name">LockedHeap</span><span class="token punctuation">::</span><span class="token function">empty</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre>
<h3 id="&#x5E94;&#x7528;&#x7A0B;&#x5E8F;&#x6A21;&#x677F;">&#x5E94;&#x7528;&#x7A0B;&#x5E8F;&#x6A21;&#x677F;</h3>
<p>&#x73B0;&#x5728;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x5C06;&#x6BCF;&#x4E00;&#x4E2A;&#x542B;&#x6709; <code>main</code> &#x51FD;&#x6570;&#x7684; Rust &#x6E90;&#x4EE3;&#x7801;&#x653E;&#x5728; <code>usr/rust/src/bin</code> &#x76EE;&#x5F55;&#x4E0B;&#x3002;&#x5B83;&#x4EEC;&#x6BCF;&#x4E00;&#x4E2A;&#x90FD;&#x4F1A;&#x88AB;&#x7F16;&#x8BD1;&#x6210;&#x4E00;&#x4E2A;&#x72EC;&#x7ACB;&#x7684;&#x53EF;&#x6267;&#x884C;&#x6587;&#x4EF6;&#x3002;</p>
<p>&#x5176;&#x6A21;&#x677F;&#x4E3A;&#xFF1A;</p>
<pre class="language-"><code class="lang-rust"><span class="token comment">// usr/rust/src/bin/model.rs</span>

<span class="token attribute attr-name">#![no_std]</span>
<span class="token attribute attr-name">#![no_main]</span>

<span class="token keyword">extern</span> <span class="token keyword">crate</span> <span class="token module-declaration namespace">alloc</span><span class="token punctuation">;</span>

<span class="token attribute attr-name">#[macro_use]</span>
<span class="token keyword">extern</span> <span class="token keyword">crate</span> <span class="token module-declaration namespace">user</span><span class="token punctuation">;</span>

<span class="token attribute attr-name">#[no_mangle]</span>
<span class="token keyword">pub</span> <span class="token keyword">fn</span> <span class="token function-definition function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">-&gt;</span> <span class="token keyword">usize</span> <span class="token punctuation">{</span>
    <span class="token number">0</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x8FD9;&#x91CC;&#x8FD4;&#x56DE;&#x7684;&#x90A3;&#x4E2A;&#x503C;&#x5373;&#x4E3A;&#x7A0B;&#x5E8F;&#x6700;&#x7EC8;&#x7684;&#x8FD4;&#x56DE;&#x503C;&#x3002;</p>
<h3 id="hello-world-&#x5E94;&#x7528;&#x7A0B;&#x5E8F;">Hello World &#x5E94;&#x7528;&#x7A0B;&#x5E8F;</h3>
<p>&#x57FA;&#x4E8E;&#x4E0A;&#x8FF0;&#x5E94;&#x7528;&#x7A0B;&#x5E8F;&#x6A21;&#x677F;&#xFF0C;&#x6211;&#x4EEC;&#x53EF;&#x4EE5;&#x5B9E;&#x73B0;&#x4E00;&#x4E2A;&#x6700;&#x7B80;&#x5355;&#x7684;<code>Hello World</code>&#x7A0B;&#x5E8F;&#xFF1A;</p>
<pre class="language-"><code class="lang-rust"><span class="token comment">// usr/rust/src/bin/hello_world.rs</span>

<span class="token attribute attr-name">#![no_std]</span>
<span class="token attribute attr-name">#![no_main]</span>

<span class="token keyword">extern</span> <span class="token keyword">crate</span> <span class="token module-declaration namespace">alloc</span><span class="token punctuation">;</span>

<span class="token attribute attr-name">#[macro_use]</span>
<span class="token keyword">extern</span> <span class="token keyword">crate</span> <span class="token module-declaration namespace">user</span><span class="token punctuation">;</span>

<span class="token attribute attr-name">#[no_mangle]</span>
<span class="token keyword">pub</span> <span class="token keyword">fn</span> <span class="token function-definition function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">-&gt;</span> <span class="token keyword">usize</span> <span class="token punctuation">{</span>
    <span class="token keyword">for</span> _ <span class="token keyword">in</span> <span class="token number">0</span><span class="token punctuation">..</span><span class="token number">10</span> <span class="token punctuation">{</span>
        <span class="token macro property">println!</span><span class="token punctuation">(</span><span class="token string">&quot;Hello world! from user mode program!&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token number">0</span>
<span class="token punctuation">}</span>
</code></pre>
<p>&#x548C;&#x5185;&#x6838;&#x9879;&#x76EE;&#x4E00;&#x6837;&#xFF0C;&#x8FD9;&#x91CC;&#x4E5F;&#x521B;&#x5EFA;&#x4E00;&#x4E2A; <code>.cargo/config</code> &#x6587;&#x4EF6;&#x6307;&#x5B9A;&#x9ED8;&#x8BA4;&#x7684;&#x76EE;&#x6807;&#x4E09;&#x5143;&#x7EC4;&#x3002;&#x4F46;&#x8FD9;&#x6B21;&#x6211;&#x4EEC;&#x5C31;&#x4E0D;&#x7528;&#x81EA;&#x5B9A;&#x4E49;&#x94FE;&#x63A5;&#x811A;&#x672C;&#x4E86;&#xFF0C;&#x7528;&#x9ED8;&#x8BA4;&#x7684;&#x5373;&#x53EF;&#x3002;</p>
<pre class="language-"><code class="lang-toml"><span class="token comment"># .cargo/config</span>

<span class="token punctuation">[</span><span class="token table class-name">build</span><span class="token punctuation">]</span>
<span class="token key property">target</span> <span class="token punctuation">=</span> <span class="token string">&quot;riscv64imac-unknown-none-elf&quot;</span>
</code></pre>
<p>&#x5207;&#x6362;&#x5230; <code>usr/rust</code> &#x76EE;&#x5F55;&#xFF0C;&#x5C31;&#x53EF;&#x4EE5;&#x8FDB;&#x884C;&#x4EA4;&#x53C9;&#x7F16;&#x8BD1;&#xFF1A;</p>
<pre class="language-"><code class="lang-bash">$ cargo build
</code></pre>
<p>&#x6211;&#x4EEC;&#x5C06;&#x80FD;&#x591F;&#x5728; <code>usr/rust/target/riscv64imac-unknown-none-elf/debug/hello_world</code> &#x770B;&#x5230;&#x6211;&#x4EEC;&#x7F16;&#x8BD1;&#x51FA;&#x6765;&#x7684;&#x53EF;&#x6267;&#x884C;&#x6587;&#x4EF6;&#xFF0C;&#x63A5;&#x4E0B;&#x6765;&#x7684;&#x95EE;&#x9898;&#x5C31;&#x662F;&#x5982;&#x4F55;&#x628A;&#x5B83;&#x52A0;&#x8F7D;&#x5230;&#x5185;&#x6838;&#x4E2D;&#x6267;&#x884C;&#x4E86;&#xFF01;</p>
<p>&#x76EE;&#x524D;&#x7684;&#x4EE3;&#x7801;&#x53EF;&#x4EE5;&#x5728;<a href="https://github.com/rcore-os/rCore_tutorial/tree/ch8-pa1" target="_blank">&#x8FD9;&#x91CC;</a>&#x627E;&#x5230;&#x3002;</p>
<p><div id="page-footer" class="localized-footer"><hr><p><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.css"></p>
<script src="https://cdn.jsdelivr.net/npm/gitalk@1/dist/gitalk.min.js"></script>
<div id="gitalk-container"></div>
<script>
  var gitalk = new Gitalk({
    clientID: "ab871063a9fa7f1da571",
    clientSecret: "4760b384e3e452d2241d859cfa6a7da21a8729ec",
    repo: "rCore_tutorial_doc",
    owner: "rcore-os",
    admin: [
      "chyyuu",
      "xyongcn",
      "wangrunji0408",
      "xy-plus",
      "PanQL",
      "wyfcyx",
      "equation314"
    ],
    id: location.pathname,
    distractionFreeMode: false
  });
  gitalk.render("gitalk-container");
</script>
</div></p>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="introduction.html" class="navigation navigation-prev " aria-label="Previous page: 第八章：进程">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="part1_1.html" class="navigation navigation-next " aria-label="Next page: 合并内核与应用程序">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"[编写用户程序]","level":"1.10.1","depth":2,"next":{"title":"合并内核与应用程序","level":"1.10.2","depth":2,"path":"chapter8/part1_1.md","ref":"chapter8/part1_1.md","articles":[]},"previous":{"title":"第八章：进程","level":"1.10","depth":1,"path":"chapter8/introduction.md","ref":"chapter8/introduction.md","articles":[{"title":"[编写用户程序]","level":"1.10.1","depth":2,"path":"chapter8/part1.md","ref":"chapter8/part1.md","articles":[]},{"title":"合并内核与应用程序","level":"1.10.2","depth":2,"path":"chapter8/part1_1.md","ref":"chapter8/part1_1.md","articles":[]},{"title":"在内核中实现系统调用","level":"1.10.3","depth":2,"path":"chapter8/part2.md","ref":"chapter8/part2.md","articles":[]},{"title":"创建虚拟内存空间","level":"1.10.4","depth":2,"path":"chapter8/part3.md","ref":"chapter8/part3.md","articles":[]},{"title":"[创建进程]","level":"1.10.5","depth":2,"path":"chapter8/part4.md","ref":"chapter8/part4.md","articles":[]},{"title":"总结与展望","level":"1.10.6","depth":2,"path":"chapter8/part5.md","ref":"chapter8/part5.md","articles":[]}]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":["hide-element","chapter-fold","katex","alerts","emphasize","-highlight","prism","localized-footer","mermaid-gb3"],"pluginsConfig":{"chapter-fold":{},"prism":{"css":["prismjs/themes/prism-tomorrow.css"]},"emphasize":{},"search":{},"localized-footer":{"filename":"extensions/comment/gitalk.html","hline":"true"},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"hide-element":{"elements":[".gitbook-link"]},"katex":{},"fontsettings":{"theme":"white","family":"sans","size":2},"mermaid-gb3":{},"alerts":{},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"chapter8/part1.md","mtime":"2020-09-09T11:31:56.899Z","type":"markdown"},"gitbook":{"version":"3.2.3","time":"2020-09-10T06:57:01.716Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>

        
    
    <script src="../gitbook/gitbook.js"></script>
    <script src="../gitbook/theme.js"></script>
    
        
        <script src="../gitbook/gitbook-plugin-hide-element/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-chapter-fold/chapter-fold.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-alerts/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-mermaid-gb3/book/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    <script src="../gitbook/gitbook-plugin-mermaid-gb3/mermaid/mermaid.min.js"></script>

    </body>
</html>

